#include "FiveWin.ch"

static aTimers := {}
static nId     := 1

//----------------------------------------------------------------------------//

CLASS TTimer

   DATA   bAction
   DATA   nId, nInterval, hWndOwner
   DATA   lActive

   METHOD New( nInterval, bAction ) CONSTRUCTOR
   METHOD Activate()
   METHOD DeActivate() BLOCK ;
          { | Self | ::lActive := .f., KillTimer( ::hWndOwner, ::hTimer ) }
   METHOD Release() BLOCK { | Self | ::DeActivate(), Self := nil }

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( nInterval, bAction ) CLASS TTimer

   DEFAULT nInterval := 18, bAction := { || nil }

   ::nInterval = nInterval
   ::bAction   = bAction
   ::nId       = nId++
   ::lActive   = .f.

   AAdd( aTimers, Self )

return

//----------------------------------------------------------------------------//

METHOD Activate() CLASS TTimer

   ::hWndOwner = GetActiveWindow()
   ::nId       = SetTimer( ::hWndOwner, ::nId, ::nInterval, 0 )
   ::lActive   = .t.

return

//----------------------------------------------------------------------------//

function TimerEvent( nId )

   local nTimer := AScan( aTimers, { | oTimer | oTimer:nId == nId } )

   if nTimer != 0
      Eval( aTimers[ nTimer ]:bAction )
   endif

return 0

//----------------------------------------------------------------------------//
